var moment = require('@/utils/moment.js');
var initData = require('./initData');
var user = require('./user.js');
export default {
    namespaced: true,
    state: {
        //业务类型
        businessNo: 1,
        /* 短租使用 */
        //订单开始时间
        startTime: '',
        //订单结束时间
        endTime: '',
        //取车类型
        rentalType: 1,
        //是否跨网点  1是 2否
        differentStoresCar: 2,

        /* 长租使用 */
        lCityId: '',
        //开始时间
        lStartTime: '',
        //结束时间
        lEndTime: '',
        //取还车门店id
        lStoreId: '',
        //取还车门店名称
        lStoreName: '',
        //租期
        lTenancyTerm: 1

    },
    mutations: {
        /* 短租 */
        saveBusinessNo(state, businessNo) {
            state.businessNo = businessNo;
        },
        saveStartTime(state, startTime) {
            state.startTime = startTime;
        },
        saveEndTime(state, endTime) {
            state.endTime = endTime;
        },
        saveRentalType(state, rentalType) {
            state.rentalType = rentalType;
        },
        saveDifferentStoresCar(state, differentStoresCar) {
            state.differentStoresCar = differentStoresCar;
        },

        /* 长租 */
        savelStartTime(state, lStartTime) {
            state.lStartTime = lStartTime;
        },
        savelEndTime(state, lEndTime) {
            state.lEndTime = lEndTime;
        },
        savelStoreId(state, lStoreId) {
            state.lStoreId = lStoreId;
        },
        savelStoreName(state, lStoreName) {
            state.lStoreName = lStoreName;
        },
        savelTenancyTerm(state, lTenancyTerm) {
            state.lTenancyTerm = lTenancyTerm;
        }

    },

    actions: {
        /* 短租获取默认租赁开始结束时间*/
        getOrderTime(context) {
            //  预留整备时间
            const delayTime = initData.default.state.initData.delayTime;
            // 是否允许营业时间服务范围外下单 1:允许 2:不允许
            const outOfTime = initData.default.state.initData.outOfTime;
            // 默认取车门店的营业时间
            const businessStartTime = user.default.state.takeStoreBusinessStartTime;
            const businessEndTime = user.default.state.takeStoreBusinessEndTime;
            //获取当前时间的分钟值(主要用于取整判断)
            let minutes = moment().minutes();
            // 默认取车时间(预留当前时间整点时间往后延后台配置的整备时间)
            let currentTime;
            let minutesStr = "";

            // 允许营业时间范围外下单
            if (outOfTime === 1) {
                if (minutes > 30) { //如果大于30->按整点取值
                    minutesStr = "00";
                    currentTime = moment().add(delayTime + 1, "hours").format("YYYY-MM-DD HH")
                } else {//如果不足30分钟->按30分钟取值
                    minutesStr = "30";
                    currentTime = moment().add(delayTime, "hours").format("YYYY-MM-DD HH")
                }
                currentTime = currentTime + ':' + minutesStr + ':' + '00';
                context.commit('saveStartTime', currentTime);

                let endTime = moment(currentTime).add(2, "days").format("YYYY-MM-DD HH:mm:ss");
                context.commit('saveEndTime', endTime);
                console.log("开始时间:" + currentTime, "结束时间:" + endTime);
            } else {//不允许
                console.log('营业时间', businessStartTime, businessEndTime);
                if (minutes > 30) { //如果大于30->按整点取值
                    minutesStr = "00";
                    currentTime = moment().add(delayTime + 1, "hours").format("YYYY-MM-DD HH")
                } else {//如果不足30分钟->按30分钟取值
                    minutesStr = "30";
                    currentTime = moment().add(delayTime, "hours").format("YYYY-MM-DD HH")
                }

                // 整备预留之后的时间
                let delayTimeafter = currentTime + ':' + minutesStr + ':' + '00';
                let delayTimeYMD = moment(delayTimeafter).format('YYYY-MM-DD');
                // 整备后时间是不是今天
                let isToday = moment(delayTimeafter).isSame(moment(),'day');

                console.log('整备后时间', delayTimeafter, isToday);
                let resultStartTime='';

                if (isToday) {
                    const currentFormatTime = moment(delayTimeafter).format('YYYY-MM-DD HH:mm');
                    const businessStartFormatTime = delayTimeYMD +' ' + businessStartTime;
                    const businessEndFormatTime = delayTimeYMD + ' ' +  businessEndTime;
                    // 是否在营业时间之前
                    console.log('对比时间',currentFormatTime, businessStartFormatTime);
                    const  isStartTimeBefore = moment(currentFormatTime).isBefore(businessStartFormatTime);
                    // 是否在营业时间之后
                    const  isEndTimeAfter = moment(currentFormatTime).isAfter(businessEndFormatTime);
                    console.log(`isStartTimeBefore${isStartTimeBefore}, isEndTimeAfter${isEndTimeAfter}`);
                    if (isStartTimeBefore){//取车时间在营业开始时间之前=>按营业开始时间取值
                        resultStartTime = delayTimeYMD + ' ' + businessStartTime + ':00';
                    }else if(isEndTimeAfter){//取车时间在营业时间结束之后=>按第二天的开始营业时间
                        resultStartTime =  moment(delayTimeYMD).add(1, "days").format("YYYY-MM-DD") +' '+ businessStartTime+':00';
                    }
                    else  {//在营业时间范围内=>按当前计算的时间取值
                        resultStartTime = delayTimeafter;
                    }
                    console.log('确定的默认时间', resultStartTime);
                    context.commit('saveStartTime', resultStartTime);
                }else  {
                    resultStartTime =  moment(delayTimeYMD).format("YYYY-MM-DD") +' '+ businessStartTime+':00';
                }

                //还车时间
                let endTime = moment(resultStartTime).add(2, "days").format("YYYY-MM-DD HH:mm:ss");
                context.commit('saveEndTime', endTime);
                console.log("开始时间:" + resultStartTime, "结束时间:" + endTime);
            }


        },

        /* 长租获取默认租赁开始结束时间*/
        getLongOrderTime(context) {
            var takeTime = moment().format("YYYY-MM-DD");
            context.commit('savelStartTime', takeTime);
            context.commit('savelTenancyTerm', 1);
            var endTime = moment().add(1, "month").format("YYYY-MM-DD")
            context.commit('savelEndTime', endTime);
        }
    }
}
